/*
 *  Copyright (C) 2012 Компания ВИТ. web: http://diasled.ru Е-mail:  vit@diasled.ru
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package ru.apertum.diasled.redactor;

import javax.swing.table.AbstractTableModel;
import ru.apertum.diasled.cfg.MyPoint;
import ru.apertum.diasled.cfg.Zone;

/**
 * 
 * @author Evgeniy Egorov
 */
public class RouteZoneTableModel extends AbstractTableModel {

    final private Zone zone;

    public RouteZoneTableModel(Zone zone) {
        this.zone = zone;
    }

    @Override
    public int getRowCount() {
        return zone.getRoute().size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return zone.getRoute().get(rowIndex).x;
            case 1:
                return zone.getRoute().get(rowIndex).y;
            default:
                throw new AssertionError();
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "X";
            case 1:
                return "Y";
            default:
                throw new AssertionError();
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return Integer.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                zone.getRoute().get(rowIndex).x = (int) aValue;
                break;
            case 1:
                zone.getRoute().get(rowIndex).y = (int) aValue;
                break;
            default:
                throw new AssertionError();
        }
    }

    public void add(int index, MyPoint point) {
        zone.getRoute().add(index, point);
        fireTableDataChanged();
    }
    
    public void addFirst(MyPoint point) {
        zone.getRoute().addFirst(point);
        fireTableDataChanged();
    }
    
    public void addLast(MyPoint point) {
        zone.getRoute().addLast(point);
        fireTableDataChanged();
    }

    public void remove(int index) {
        zone.getRoute().remove(index);
        fireTableDataChanged();
    }

    public void remove(MyPoint point) {
        zone.getRoute().remove(point);
        fireTableDataChanged();
    }
}
